/**
 * Filename: SwitchPhotoAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/5/8 15:25
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.List;

public class SwitchPhotosAdapter extends RecyclerView.Adapter<SwitchPhotosAdapter.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private View mHeaderView;
    private Context mContext;
    private List<String> mDatas;

    public SwitchPhotosAdapter(Context mContext, List<String> mDatas){
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    public  class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView iv_group_grid_image;
        ImageView        iv_group_grid_delete_image;

        public ViewHolder(final View itemView) {
            super(itemView);
            iv_group_grid_image	= (SimpleDraweeView) itemView
                    .findViewById(R.id.iv_group_grid_image);
            iv_group_grid_delete_image = (ImageView) itemView
                    .findViewById(R.id.iv_group_grid_delete_image);

            itemView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    ViewGroup.LayoutParams vlp = itemView.getLayoutParams();
                    vlp.height = itemView.getMeasuredWidth();
                    vlp.width = itemView.getMeasuredWidth();
                    itemView.setLayoutParams(vlp);
                    return true;
                }
            });
        }

        public void bindData(final Uri item, final int position)
        {
            if (item != null)
            {
                Uri uri = Uri.parse("file://"+item);
                ImageOptHelper.showFrescoNative(uri, iv_group_grid_image, mContext);
                // 删除
                iv_group_grid_delete_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        System.out.println(SelectedImg.mSelectedImage.size()+"==============="+position);
                        mDatas.remove(position);
                        SelectedImg.mSelectedImage.remove(position);
//                        notifyDataSetChanged();
//                        notifyItemRemoved(position+1);
                        notifyItemRangeRemoved(0, mDatas.size()+1);
                        notifyItemRangeInserted(0, mDatas.size());
                    }
                });
            }
        }
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }
    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public int getItemViewType(int position) {
        if(mHeaderView == null) return TYPE_NORMAL;
        if(position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    private String getItem(int position) {
        return mDatas.get(position);
    }
    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_grid_image, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        String listItems = getItem(pos);
        Uri item = Uri.parse(listItems);
        holder.bindData(item , pos);
        holder.itemView.setTag(listItems);
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? mDatas.size() : mDatas.size() + 1;
    }
}
